<template>
	<view class="container">
		<scroll-view scroll-y class="wish-list" @refresherrefresh="onRefresh" refresher-enabled>
			<view class="wish-card" v-if="wishList.length === 0">
				<view class="empty-state">
					<text>暂无心愿，快来发布第一个吧！</text>
				</view>
			</view>
			
			<view class="wish-items" v-else>
				<view 
					class="wish-item" 
					v-for="(item, index) in wishList" 
					:key="index"
				>
					<view class="wish-header">
						<image class="avatar" :src="item.avatar || '/static/default-avatar.png'"></image>
						<view class="user-info">
							<text class="username">{{maskUsername(item.username)}}</text>
							<text class="time">{{formatTime(item.created_at)}}</text>
						</view>
					</view>
					<view class="wish-content">
						<text>{{item.content}}</text>
					</view>
				</view>
			</view>
		</scroll-view>
		
		<view class="publish-btn" @click="navigateToPublish">
			<text>+</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				wishList: [],
				isLoading: false
			}
		},
		onLoad() {
			this.loadWishList();
		},
		onShow() {
			// 页面显示时检查登录状态
			const token = uni.getStorageSync('token');
			if (!token) {
				uni.reLaunch({
					url: '/pages/login/login'
				});
			} else {
				this.loadWishList();
			}
		},
		methods: {
			loadWishList() {
				if (this.isLoading) return;
				
				this.isLoading = true;
				uni.showLoading({
					title: '加载中...'
				});
				
				this.$api.getWishList().then(res => {
					console.log('获取心愿列表成功:', res);
					this.wishList = res;
				}).catch(err => {
					console.error('获取心愿列表失败:', err);
					uni.showToast({
						title: '获取心愿列表失败',
						icon: 'none',
						duration: 2000
					});
				}).finally(() => {
					this.isLoading = false;
					uni.hideLoading();
				});
			},
			
			onRefresh() {
				this.loadWishList();
				setTimeout(() => {
					uni.stopPullDownRefresh();
				}, 1000);
			},
			
			navigateToPublish() {
				uni.navigateTo({
					url: '/pages/publish/publish'
				});
			},
			
			viewWishDetail(id) {
				// 暂时不需要跳转到详情
				// uni.navigateTo({
				// 	url: `/pages/wish-detail/wish-detail?id=${id}`
				// });
			},
			
			formatTime(timestamp) {
				const date = new Date(timestamp);
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0');
				const day = String(date.getDate()).padStart(2, '0');
				const hour = String(date.getHours()).padStart(2, '0');
				const minute = String(date.getMinutes()).padStart(2, '0');
				return `${year}-${month}-${day} ${hour}:${minute}`;
			},
			
			maskUsername(username) {
				if (!username) return '';
				
				if (username.length <= 1) {
					return username;
				} else {
					return username.charAt(0) + '*'.repeat(username.length - 1);
				}
			}
		}
	}
</script>

<style>
	.container {
		display: flex;
		flex-direction: column;
		min-height: 100vh;
		background-color: #f5f7fa;
		padding: 30rpx;
		box-sizing: border-box;
	}
	
	.wish-list {
		flex: 1;
	}
	
	.wish-card {
		background-color: #ffffff;
		border-radius: 20rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
		overflow: hidden;
		margin-bottom: 30rpx;
	}
	
	.wish-items {
		padding: 20rpx;
	}
	
	.empty-state {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 200rpx;
		color: #999;
		font-size: 28rpx;
		padding: 30rpx;
	}
	
	.wish-item {
		background-color: #ffffff;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 4rpx 15rpx rgba(0, 0, 0, 0.08);
		transition: all 0.3s ease;
	}
	
	.wish-item:active {
		transform: scale(0.98);
	}
	
	.wish-header {
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;
	}
	
	.avatar {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		margin-right: 20rpx;
		background-color: #f0f2f5;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
	}
	
	.user-info {
		display: flex;
		flex-direction: column;
	}
	
	.username {
		font-size: 30rpx;
		font-weight: bold;
		color: #333;
	}
	
	.time {
		font-size: 24rpx;
		color: #999;
		margin-top: 4rpx;
	}
	
	.wish-content {
		font-size: 32rpx;
		color: #333;
		line-height: 1.5;
	}
	
	.publish-btn {
		position: fixed;
		right: 40rpx;
		bottom: 100rpx;
		width: 100rpx;
		height: 100rpx;
		background: linear-gradient(135deg, #3b73d9, #6159e6);
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		box-shadow: 0 8rpx 20rpx rgba(97, 89, 230, 0.25);
		border: none;
		transition: all 0.3s ease;
	}
	
	.publish-btn:active {
		transform: scale(0.95);
		box-shadow: 0 4rpx 10rpx rgba(97, 89, 230, 0.2);
	}
	
	.publish-btn text {
		color: #fff;
		font-size: 60rpx;
		font-weight: bold;
		margin-bottom: 5rpx;
	}
</style>
